package rs.arm.letthemusicplay;

import java.util.ArrayList;

import rs.arm.letthemusicplay.utilities.Constants;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PlayListActivity extends ListActivity {

	private ArrayList<String> songsWithPaths;
	private ArrayList<String> songs;
	private String songName = "...";
	private int songNumber = -1;
	private boolean isClicked = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.play_list);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		Intent i = getIntent();
		songsWithPaths = i.getStringArrayListExtra(Constants.EXTRA_PLAY_LIST);
		fillSongNames();
		PlayListAdapter adapter = new PlayListAdapter(this, songs);
		final ListView listView = (ListView) findViewById(R.id.list_of_songs);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				PlayListAdapter adapter = (PlayListAdapter) listView.getAdapter();
				songName = (String) adapter.getItem(arg2);
				songNumber = arg2;
				isClicked = true;
				finish();
			}
		});
	}

	private void fillSongNames() {
		songs = new ArrayList<String>();
		for (int i = 0; i < songsWithPaths.size(); i++) {
			String s = songsWithPaths.get(i);
			s = s.substring(s.lastIndexOf("/") + 1);
			songs.add(s);
		}
	}

	@Override
	public void finish() {
		if (isClicked) {
			isClicked = false;
			Intent data = new Intent();
			data.putExtra(Constants.EXTRA_SONG_NAME, songName);
			data.putExtra(Constants.EXTRA_SONG_NUMBER, songNumber);
			data.putStringArrayListExtra(Constants.EXTRA_PLAY_LIST, songsWithPaths);
			setResult(RESULT_OK, data);
		} else {
			setResult(RESULT_CANCELED);
		}
		super.finish();
	}

	private class PlayListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private ArrayList<String> list;

		public PlayListAdapter(Context context, ArrayList<String> list) {
			mInflater = LayoutInflater.from(context);
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			final ListHolder holder;
			View v = arg1;
			if (v == null || v.getTag() == null) {
				v = mInflater.inflate(R.layout.play_list_item, null);
				holder = new ListHolder();
				holder.textView = (TextView) v.findViewById(R.id.play_list_item_name);
				v.setTag(holder);
			} else {
				holder = (ListHolder) v.getTag();
			}
			holder.name = (String) getItem(arg0);
			holder.textView.setText(holder.name);
			v.setTag(holder);
			return v;
		}

		public class ListHolder {
			String name;
			TextView textView;
		}
	}
}
